package com.epfresh.supplier.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.epfresh.supplier.R;
/**
 * Created by zhuli on 2017/8/17.
 */

public class BaseItemDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDivider;
    private int leftOffset = 0;

    /**
     * RecyclerView 项目之间的线条
     *
     * @param context 上下文
     */
    public BaseItemDecoration(Context context) {
        this(context, R.drawable.shape_line);
    }

    public BaseItemDecoration(Context context, int line) {
        mDivider = ContextCompat.getDrawable(context, line);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left + leftOffset, top, right, bottom);
            mDivider.draw(c);
        }
    }
}
